package net.homeip.jcperera.nu.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.ejb.EJB;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.homeip.jcperera.nu.core.boot.Boot;
import net.homeip.jcperera.nu.people.person.Gender;
import net.homeip.jcperera.nu.people.person.Person;

/**
 * Servlet implementation class StartupServlet
 */
@WebServlet(description = "Startup Servlet (Boot)", urlPatterns = { "/startup" }, initParams = {
@WebInitParam(name = "nu.db.class", value = "oracle.jdbc.driver.OracleDriver", description = "NU Database JDBC Class Name"),
@WebInitParam(name = "nu.db.url", value = "jdbc:oracle:thin:@nuhost.jcperera.homeip.net:1521:NUAPPDB", description = "NU Database JDBC URL"),
@WebInitParam(name = "nu.db.user", value = "nuapp", description = "NU Database Username"),
@WebInitParam(name = "nu.db.password", value = "nuapp", description = "NU Database Password") }, loadOnStartup = 1)
public class StartupServlet extends HttpServlet implements Servlet
{

    private static final long serialVersionUID = 9079293290031048643L;
    private ServletConfig     config           = null;

    @EJB
    Boot                      boot;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public StartupServlet()
    {
        super();
    }

    /**
     * @see Servlet#init(ServletConfig)
     */
    public void init(ServletConfig config) throws ServletException
    {
        this.config = config;
        try
        {
            boot.start(getInitParameter("nu.db.class"), getInitParameter("nu.db.url"), getInitParameter("nu.db.user"),
                    getInitParameter("nu.db.password"));
        }
        catch (Exception e)
        {
            throw new ServletException(e);
        }
    }

    /**
     * @see Servlet#destroy()
     */
    public void destroy()
    {
        boot.stop();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        // Output this value to the browser that called this servlet.
        response.setContentType("text/html; charset=ISO-8859-1");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head><title>SimpleJPATest</title></head>");
        out.println("<body>");
        out.println("<p>Starting...");

        Person person = new Person();
        person.setPersonID("JUANLOPEZ1");
        person.setFirstName("Juan");
        person.setMiddleInitial("C");
        person.setLastName("Federico");
        person.setSurName("Sanchez");
        person.setOtherSurName("Lopez");
        person.setGender(Gender.MALE);

        Person person2 = new Person();
        person2.setPersonID("JUANLOPEZ2");
        person2.setFirstName("Juan");
        person2.setMiddleInitial("C");
        person2.setLastName("Federico");
        person2.setSurName("Sanchez");
        person2.setOtherSurName("Lopez");
        person2.setGender(Gender.MALE);

        EntityManagerFactory emf = Boot.getSystem().getPersistence().getEntityManagerFactory();
        EntityManager entityManager = (EntityManager) emf.createEntityManager();
        entityManager.getTransaction().begin();
        entityManager.persist(person);
        entityManager.persist(person2);

        entityManager.getTransaction().commit();

        entityManager.getTransaction().begin();

        person.setMiddleInitial("F");
        entityManager.persist(person);

        entityManager.getTransaction().commit();

        entityManager.close();

        out.println("done</p>");
        out.println("</body></html>");
        out.close();
    }

    /**
     * @see Servlet#getServletConfig()
     */
    public ServletConfig getServletConfig()
    {
        return this.config;
    }

    /**
     * @see Servlet#getServletInfo()
     */
    public String getServletInfo()
    {
        return null;
    }

}
